package com.bawei.module_login.view.fragment

import android.widget.Button
import androidx.fragment.app.Fragment
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.module_base.net.bean.login.FindLabelsBean
import com.bawei.module_base.ui.base.BaseFragment
import com.bawei.module_login.R
import com.bawei.module_login.databinding.FragmentLabelBinding
import com.bawei.module_login.viewmodel.LabelViewModel


class LabelFragment : BaseFragment<LabelViewModel, FragmentLabelBinding>() {
    //创建一个map
    var map = HashMap<String, String>()
    var labs= mutableListOf<FindLabelsBean>()
    lateinit var b1:Button
    override fun initData() {
        dataBinding.btnPlaySquare.setOnClickListener {
            //添加标签的网络请求判断 条件
            if (map.size != 0) {
                //网络请求
                viewModel.postSaveTag(map)
            }

            //跳转到广场
            ARouter.getInstance()
                .build("/main/main_activity")
                .navigation()
            //回退
            requireActivity().finish()

        }


        viewModel.getRequestLabel()
        viewModel.dataLabel.observe(this){
            it.result.let {
                it1->
                if (it1 != null) {
                    labs.addAll(it1)
                }
            }
            labs.forEach{i->
                b1.setText(i.name)
                dataBinding.flSelected.addView(b1)
            }
        }

    }

        override fun getViewModelClass(): Class<LabelViewModel> {
            return LabelViewModel::class.java
        }

        override fun getLayout(): Int {
            return R.layout.fragment_label
        }
}